//
//  OPBoardConfig.m
//  OPFramework
//
//  Created by malcom on 7/16/08.
//  Copyright 2008 malcom-mac software. All rights reserved.
//

#import "OPBoardConfig.h"
#import "OPBoard.h"

@implementation OPBoardConfig

- (id) initWithBoard:(OPBoard *) _board {
	self = [super initWithBoard: _board];
	if (self != nil) {
		// failed ot load board configuration
		if (! [self loadData] ) return nil;
	}
	return self;
}

- (BOOL) loadData {
	// make the request
	/*XMLRPCResponse *boardConfig = [_delegateBoard _executeRequestMethodNamed: @"miscs.getBoardConfigurationData" 
																  withParams: nil
															  attachAuthData: NO];
	
	BOOL resultOK = ![boardConfig isFault];
	if (resultOK) // if ok we can update the class dictionary
		[self _updateConfigWithData: (NSDictionary*)[boardConfig responseObject]];
	
	NSLog(@"Load board configuration '%@'... %@",[self getPropertyForKey: OPBoardConfig_Sitename],(resultOK ? @"OK" : @"FAILED"));
	return resultOK;*/
	
	BOOL res = [self sendXMLRequestForMethod: @"miscs.getBoardConfigurationData" withParams:nil parseSelector:@selector(_updateConfigWithData:) forceSyncRequest: NO];
	return res;
}

- (void) _updateConfigWithData:(NSArray *) _result {
	_configurationData = [[NSMutableDictionary alloc] initWithDictionary: (NSDictionary*)[_result objectAtIndex:0]];
	
	// convert strings to integer
//	#define _ConfigIntegerConvertKeys  [NSArray arrayWithObjects:OPBoardConfig_PostsPerPage, OPBoardConfig_TopicsPerPage,OPBoardConfig_NumFiles,OPBoardConfig_NumPosts,OPBoardConfig_NumTopics,OPBoardConfig_NumUsers,OPBoardConfig_RecordOnlineUsers,OPBoardConfig_OnlineGuests,OPBoardConfig_OnlineUsers,OPBoardConfig_OnlineTimeStats,nil]
//	for (NSString *cKey in _ConfigIntegerConvertKeys)
//		[_configurationData setObject: [NSNumber numberWithInt: [[_configurationData objectForKey: cKey] intValue]] forKey:cKey];
	
	// record date
	[_configurationData setObject: [NSDate dateWithTimeIntervalSince1970: [[_configurationData objectForKey:OPBoardConfig_RecordOnlineDate] longLongValue]]
					 forKey:OPBoardConfig_RecordOnlineDate];
}

- (void)connection: (XMLRPCConnection *)connection didReceiveResponse:(XMLRPCResponse *)response forMethod: (NSString *)method {
	// super class check if it's an error, a numeric response or simply some data to parse
	if (![self _checkErrorInRawResponseFromServer: response forMethod:method]) {
		// data update to parse
		[self _updateConfigWithData: [NSArray arrayWithObjects: [response responseObject],method,nil]];
		// we can propagate the result to listeners
		[self dataUpdatedFor: self atMethod:method];	
	}
}

- (BOOL) _isNumericResponseFromServerAnErrorCode:(NSNumber *) _code forMethod:(NSString *) method {
	return NO;
}

- (id) getPropertyForKey:(NSString *) key {
	return [_configurationData valueForKey: key];
}

- (void) dealloc {
	[_configurationData release];
	[super dealloc];
}


@end
